﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Fasterflect;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;

namespace Kugar.UI.RazorUI.CefEngine
{
    internal static class RazorResourceLoader
    {
        public static IRazorUIResourceHandler FindHandler(string path)
        {
            //后续优化掉,放入缓存
            var type = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).FirstOrDefault(x =>
                x.IsImplementlInterface(typeof(IRazorUIResourceHandler))&& x.GetCustomAttributes(typeof(RazorForViewAttribute)).Any(y => ((RazorForViewAttribute)y).Url.CompareTo(path, true)));

            if (type==null)
            {
                return null;
            }

            return (IRazorUIResourceHandler)type.CreateInstance(path);

        }

        public static bool ExistsHandler(string path)
        {
            //后续优化掉,放入缓存
            return AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Any(x =>
                x.IsImplementlInterface(typeof(IRazorUIResourceHandler)) && x.GetCustomAttributes(typeof(RazorForViewAttribute)).Any(y => ((RazorForViewAttribute)y).Url.CompareTo(path, true)));

        }
    }

    
}
